//
//  ShouGongGoodListVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/12.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ShouGongGoodListVC.h"
#import "BillAddPackageGoodModel.h"
#import "ShouGongGoodListCell.h"
#import "ShouDongGoodDetailVC.h"

@interface ShouGongGoodListVC () <UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <BillAddPackageGoodModel *>*dataArray;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation ShouGongGoodListVC

-(NSMutableArray <BillAddPackageGoodModel *>*)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - self.bottomView.nim_height - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"选择完成" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:nextBtn];
    }
    return _bottomView;
}

- (void)sureClick {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定提交吗？" preferredStyle:UIAlertControllerStyleAlert];
    __weak_self;
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakself.rt_navigationController popViewControllerAnimated:YES];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancleAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"手工绑定";
    page = 1;
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForList];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:ReceiptDeliveryRefreshShouGongAddGoodList object:nil];
}

- (void)refreshData {
    page = 1;
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:ReceiptDeliveryBillAddPackageShouGongGoodList params:@{@"wa_id":self.wa_id,@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [BillAddPackageGoodModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
            }else {
                NSArray *array = [BillAddPackageGoodModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.dataArray addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShouGongGoodListCell *cell = [ShouGongGoodListCell ShouGongGoodListCellWithTableView:tableView];
    BillAddPackageGoodModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    __weak_self;
    cell.operationBlock = ^{
        ShouDongGoodDetailVC *vc = [ShouDongGoodDetailVC new];
        vc.g_id = model.g_id;
        vc.wa_id = weakself.wa_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShouGongGoodListCell *cell = [ShouGongGoodListCell ShouGongGoodListCellWithTableView:tableView];
    BillAddPackageGoodModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
